package com.jai.pms.service.impl;

import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.Data;
import com.jai.pms.domain.Project;
import com.jai.pms.mapper.DataMapper;
import com.jai.pms.mapper.ProjectMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.ProjectDataMapper;
import com.jai.pms.domain.ProjectData;
import com.jai.pms.service.IProjectDataService;

/**
 * 项目数据管理Service业务层处理
 *
 * @author v
 * @date 2023-09-21
 */
@Service
public class ProjectDataServiceImpl implements IProjectDataService {
    @Autowired
    private ProjectDataMapper projectDataMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private DataMapper dataMapper;

    /**
     * 查询项目数据管理
     *
     * @param id 项目数据管理主键
     * @return 项目数据管理
     */
    @Override
    public ProjectData selectProjectDataById(Long id) {
        return projectDataMapper.selectProjectDataById(id);
    }

    /**
     * 查询项目数据管理列表
     *
     * @param projectData 项目数据管理
     * @return 项目数据管理
     */
    @Override
    public List<ProjectData> selectProjectDataList(ProjectData projectData) {
        return projectDataMapper.selectProjectDataList(projectData);
    }

    /**
     * 新增项目数据管理
     *
     * @param projectData 项目数据管理
     * @return 结果
     */
    @Override
    public int insertProjectData(ProjectData projectData) {
        preCheck(projectData, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectData.setCreateBy(loginUser.getUsername());
        projectData.setCreateTime(DateUtils.getNowDate());

        return projectDataMapper.insertProjectData(projectData);
    }

    /**
     * 修改项目数据管理
     *
     * @param projectData 项目数据管理
     * @return 结果
     */
    @Override
    public int updateProjectData(ProjectData projectData) {
        preCheck(projectData, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectData.setUpdateBy(loginUser.getUsername());
        projectData.setUpdateTime(DateUtils.getNowDate());

        return projectDataMapper.updateProjectData(projectData);
    }

    /**
     * 批量删除项目数据管理
     *
     * @param ids 需要删除的项目数据管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectDataByIds(Long[] ids) {
        return projectDataMapper.deleteProjectDataByIds(ids);
    }

    /**
     * 删除项目数据管理信息
     *
     * @param id 项目数据管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectDataById(Long id) {
        return projectDataMapper.deleteProjectDataById(id);
    }


    /**
     * 添加、修改前检查
     *
     * @param projectData 数据
     * @param isAdd       是否为添加
     * @return 返回结果数据
     */
    private ProjectData preCheck(ProjectData projectData, boolean isAdd) {
        // 设置默认父类id为0
        if (projectData.getParentId() == null) {
            projectData.setParentId(0l);
        }

        // 填充parent信息
        if (projectData.getParentId() != 0) {
            // 检查父类projectData是否存在
            ProjectData parentProjectData = new ProjectData();
            parentProjectData.setProjectId(projectData.getProjectId());
            parentProjectData.setDataId(projectData.getParentId());
            List<ProjectData> projectDataList = projectDataMapper.selectProjectDataList(parentProjectData);
            if (projectDataList.size() != 1) {
                throw new ServiceException("所选的父类在该项目中不存在");
            }

            projectData.setParentCode(projectDataList.get(0).getDataCode());
            projectData.setParentName(projectDataList.get(0).getDataName());
            projectData.setAncestors(projectDataList.get(0).getAncestors() + "," + projectDataList.get(0).getDataId());
        } else {
            // 在父类被设置为无时，需要清空对应code和name
            projectData.setParentCode("");
            projectData.setParentName("");
            projectData.setAncestors("0");
        }

        // 检查是否有重复信息，联合主键projectId和dataId
        ProjectData query = new ProjectData();
        query.setProjectId(projectData.getProjectId());
        query.setDataId(projectData.getDataId());
        List<ProjectData> queryList = projectDataMapper.selectProjectDataList(query);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该数据在项目中已经存在");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), projectData.getId())) {
                throw new ServiceException("该数据在项目中已经存在");
            }
        }

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(projectData.getProjectId());
        projectData.setProjectCode(project.getCode());
        projectData.setProjectName(project.getName());

        // 填充data的code、name信息
        Data data = dataMapper.selectDataById(projectData.getDataId());
        projectData.setDataCode(data.getCode());
        projectData.setDataName(data.getName());

        return projectData;
    }
}
